import Vue from 'vue'
import Vuex from 'vuex'
import geo from './modules/geo'
import home from './modules/home'

Vue.use(Vuex)

const store = () => new Vuex.Store({
  modules: {
    geo,
    home
  },
  actions: {
    async nuxtServerInit({commit}, {req, app}) {
      // setPosition
      const {
        status: positionStatus, 
        data: {
          province, 
          city
        }
      } = await app.$axios.get('/geo/getPosition')

      commit('geo/setPosition', positionStatus === 200 ? {city, province} : {city: '', province: ''})

      // setHotCity
      const {status: hotCityStatus, data: {hots}} = await app.$axios.get('/geo/hotCity')
      commit('geo/setHotCity', hotCityStatus === 200 ? hots : [])

      // setMenu
      const {status: menuStatus, data: {menu}} = await app.$axios.get('/geo/menu')
      commit('home/setMenu', menuStatus === 200 ? menu : [])

      // hotPlace
      commit('home/setHotPlace', app.store.state.geo.position.city.replace('市', ''))
    }
  }
})

export default store
